package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl

/**
 * @author flutterdash@qq.com
 * @since 2021/7/4 15:52
 */
class P88 : TestDsl {

    private fun merge(nums1: IntArray, m: Int,
                      nums2: IntArray, n: Int) {
        var pos = nums1.size - 1
        var mPos = m - 1
        var nPos = n - 1
        while (mPos >= 0 && nPos >= 0) {
            if (nums1[mPos] >= nums2[nPos]) {
                nums1[pos] = nums1[mPos]
                mPos--
            } else {
                nums1[pos] = nums2[nPos]
                nPos--
            }
            pos--
        }

        if (nPos >= 0) {
            while (nPos >= 0) {
                nums1[pos] = nums2[nPos]
                nPos--
                pos--
            }
        }
    }

    @Test
    fun test0() {
        val num1 = intArrayOf(1, 2, 3, 0, 0, 0)
        val num2 = intArrayOf(2, 5, 6)
        merge(num1, 3, num2, 3)
        num1.log()
    }

    @Test
    fun test1() {
        val num1 = intArrayOf(0)
        val num2 = intArrayOf(1)
        merge(num1, 0, num2, 1)
        num1.log()
    }

}
